package com.gx.smart.building.ui.spare

import android.content.Intent
import android.view.ViewGroup
import com.gx.smart.base.BaseAdapter
import com.gx.smart.base.BaseMVVMRefreshActivity
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivitySelectAreaBinding
import com.gx.smart.building.ui.spare.adapter.WareHouseAdapter
import com.gx.smart.building.ui.spare.model.WarehouseMessageEvent
import com.gx.smart.building.ui.spare.viewmodel.SparePartViewModel
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.warehouse.v1.WarehouseDto
import com.scwang.smartrefresh.layout.api.RefreshLayout
import org.greenrobot.eventbus.EventBus

class SelectAreaActivity : BaseMVVMRefreshActivity<SparePartViewModel>(),
    BaseAdapter.OnItemClickListener<WarehouseDto> {
    private lateinit var viewBinding: ActivitySelectAreaBinding
    private lateinit var adapter: WareHouseAdapter

    override fun onBindViewModel(): Class<SparePartViewModel> = SparePartViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_select_area

    override fun getToolbarTitle(): String = "仓库选择"

    override fun initContentView(root: ViewGroup) {
        viewBinding = ActivitySelectAreaBinding.inflate(layoutInflater, root, true)
        adapter = WareHouseAdapter(this)
        adapter.setItemClickListener(this)
        initView(viewBinding.recyclerView, adapter, viewBinding.refreshLayout)
    }

    override fun initData() {
        viewModel.queryWarehouseList(this.currentPage).observe(this) { list ->
            handleResult(list)
        }
    }

    override fun onRefresh(refreshlayout: RefreshLayout) {
        super.onRefresh(refreshlayout)
        initData()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        super.onLoadMore(refreshLayout)
        initData()
    }

    override fun onItemClick(item: WarehouseDto, position: Int) {
        startActivity(Intent(this, SparePartActivity::class.java))
        EventBus.getDefault().postSticky(WarehouseMessageEvent(item))
    }


}